/*
 * Copyright 2011 Powersi. All rights reserved.
 */
package com.powersi.sample.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.powersi.hygeia.framework.BaseDAOImpl;
import com.powersi.hygeia.framework.util.DAOHelper;
import com.powersi.hygeia.framework.util.DBHelper;
import com.powersi.hygeia.framework.util.UtilFunc;

/**
 * The Class SampleDAOImpl.
 */
public class SampleDAOImpl extends BaseDAOImpl implements SampleDAO {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.powersi.sample.dao.SampleDAO#queryCatalog(java.lang.String,
	 * java.lang.String, java.lang.String)
	 */
	public List queryCatalog(String centerId, String queryType,
			String queryValue) {
		StringBuffer lSQL = new StringBuffer();

		lSQL.append(" select * ");
		lSQL.append("   from ka20 ");
		lSQL.append("  where aaa027 = ? ");
		lSQL.append("    and aae030 <= to_number(to_char(sysdate, 'yyyymmdd')) ");
		lSQL.append("    and aae031 >= to_number(to_char(sysdate, 'yyyymmdd')) ");
		lSQL.append("    and aae016 = '1' ");

		String search = UtilFunc.trimSqlParam(queryValue);
		lSQL.append(" and ");
		if ("code".equals(queryType)) {
			lSQL.append("ake001");
		} else if ("name".equals(queryType)) {
			lSQL.append("ake002");
		} else if ("py".equals(queryType)) {
			lSQL.append("aka020");
		} else if ("wb".equals(queryType)) {
			lSQL.append("aka021");
		} else {
			lSQL.append("aka020");
		}

		lSQL.append(" like ?");

		lSQL.append(" order by ake002, ake001");
		return DBHelper.executeList(lSQL.toString(),
				new Object[] { centerId, "%" + search.toUpperCase() + "%" });
	}

	/* (non-Javadoc)
	 * @see com.powersi.sample.dao.SampleDAO#queryCodetable(java.util.Map)
	 */
	public List queryCodetable(Map map) {
		ArrayList params = new ArrayList();
		
		StringBuffer lSQL = new StringBuffer();

		lSQL.append(" select * ");
		lSQL.append("   from aa10 ");
		lSQL.append("   where 1 = 1 ");
		
		for(String str : DAOHelper.getTableColumns("aa10")){
			String value = UtilFunc.trimSqlParam(map.get(str));
	        if (UtilFunc.hasLength(value)) {
	            lSQL.append("    and ").append(str).append(" like ? ");
	            params.add("%" + value.toUpperCase() + "%");
	        }
		}
		
		lSQL.append(" order by aaa100, aaa102");

		return DBHelper.executeList(lSQL.toString(), UtilFunc.toArray(params));
	}

}
